package com.turbo.cloud.core.annotations.datamasking.processor;


import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.DesensitizedUtil;
import com.turbo.cloud.core.annotations.datamasking.DataMaskingType;
import lombok.extern.slf4j.Slf4j;

import static com.turbo.cloud.core.annotations.datamasking.DataMaskingType.HIDE;
import static com.turbo.cloud.core.annotations.datamasking.DataMaskingType.USER_ID;

/**
 * 数据脱敏执行类
 *
 * @author zhangluning
 */
@Slf4j
public class DataMaskingProcessor {

    /**
     * 数据脱敏
     *
     * @param dataMaskingType 脱敏类型
     * @param value           需要脱敏的值
     */
    public static String process(DataMaskingType dataMaskingType, String value) {
        return process(dataMaskingType, value, new int[]{0, 0});
    }

    /**
     * 数据脱敏
     *
     * @param dataMaskingType 脱敏类型
     * @param value           需要脱敏的值
     * @param hideIdxArr      隐藏索引
     */
    public static String process(DataMaskingType dataMaskingType, String value, int[] hideIdxArr) {
        if (HIDE.equals(dataMaskingType)) {
            if (hideIdxArr.length == 0) {
                return value;
            }
        }
        return switch (dataMaskingType) {
            //userId
            case USER_ID -> DesensitizedUtil.desensitized(value, DesensitizedUtil.DesensitizedType.USER_ID);
            //中文名
            case CHINESE_NAME -> DesensitizedUtil.desensitized(value, DesensitizedUtil.DesensitizedType.CHINESE_NAME);
            //身份证号
            case ID_CARD -> DesensitizedUtil.desensitized(value, DesensitizedUtil.DesensitizedType.ID_CARD);
            //座机
            case FIXED_PHONE -> DesensitizedUtil.desensitized(value, DesensitizedUtil.DesensitizedType.FIXED_PHONE);
            //手机号
            case MOBILE_PHONE -> DesensitizedUtil.desensitized(value, DesensitizedUtil.DesensitizedType.MOBILE_PHONE);
            //地址
            case ADDRESS -> DesensitizedUtil.desensitized(value, DesensitizedUtil.DesensitizedType.ADDRESS);
            //邮箱
            case EMAIL -> DesensitizedUtil.desensitized(value, DesensitizedUtil.DesensitizedType.EMAIL);
            // 银行卡
            case BANK_CARD -> DesensitizedUtil.desensitized(value, DesensitizedUtil.DesensitizedType.BANK_CARD);
            //密码
            case PASSWORD -> DesensitizedUtil.desensitized(value, DesensitizedUtil.DesensitizedType.PASSWORD);
            //中国大陆车牌号
            case CAR_LICENSE -> DesensitizedUtil.desensitized(value, DesensitizedUtil.DesensitizedType.CAR_LICENSE);
            // IPV4
            case IPV4 -> DesensitizedUtil.desensitized(value, DesensitizedUtil.DesensitizedType.IPV4);
            // IPV6
            case IPV6 -> DesensitizedUtil.desensitized(value, DesensitizedUtil.DesensitizedType.IPV6);
            // 隐藏字符
            case HIDE -> CharSequenceUtil.hide(value, hideIdxArr[0], hideIdxArr[1]);
            // 原字符
            default -> value;
        };
    }

}
